const app= getApp();
var fall = require('../../fall.js');
Page({
    data: {
     circleList: [],//圆点数组
     awardList: [],//奖品数组
     colorCircleFirst: '#FFDF2F',//圆点颜色1
     colorCircleSecond: '#FE4D32',//圆点颜色2
     colorAwardDefault: '#F5F0FC',//奖品默认颜色
     colorAwardSelect: '#ffe400',//奖品选中颜色
     indexSelect: 0,//被选中的奖品index
     isRunning: false,//是否正在抽奖
     index:5,//中奖的索引  这个是拿到后端接口后赋的值，第几个奖品
     times: 3, //转盘转的次数
     open:-1,
     limit:0

    },
    
    onLoad: function () {
        let that=this;
        var open=wx.getStorageSync('open');
        that.setData({
            platform:app.globalData.platform,    
            open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl,
            uploadUrl:app.globalData.uploadUrl,
          })
   
    
    },
    goBack:function(){
        wx.navigateBack();
      },
    onShow(){
       
        this.getLottery();
    },
    getLottery:function(){
        let that = this;
        var open=wx.getStorageSync('open');
        wx.request({
            url: app.globalData.platUrl+'lottery/getLottery',
            data:{from:1,user_id:open.user_id},
            success:res=>{
                //console.log(res);
                var data=res.data;
                if(res.statusCode==200){
                    that.setData({
                        imageAward:data.data,
                        back:data.back,
                        limit:data.limit
                    })
                    that.setLottery();
                }
            }
        })
    },
    setLottery:function(){
        var _this = this;
        //圆点设置
        var leftCircle = 7.5;
        var topCircle = 7.5;
        var circleList = [];
        for (var i = 0; i < 24; i++) {
         if (i == 0) {
          topCircle = 15;
          leftCircle = 15;
         } else if (i < 6) {
          topCircle = 7.5;
          leftCircle = leftCircle + 102.5;
         } else if (i == 6) {
          topCircle = 15
          leftCircle = 620;
         } else if (i < 12) {
          topCircle = topCircle + 94;
          leftCircle = 620;
         } else if (i == 12) {
          topCircle = 565;
          leftCircle = 620;
         } else if (i < 18) {
          topCircle = 570;
          leftCircle = leftCircle - 102.5;
         } else if (i == 18) {
          topCircle = 565;
          leftCircle = 15;
         } else if (i < 24) {
          topCircle = topCircle - 94;
          leftCircle = 7.5;
         } else {
          return
         }
         circleList.push({ topCircle: topCircle, leftCircle: leftCircle });
        }
        this.setData({
         circleList: circleList
        })
        //圆点闪烁
        setInterval(function () {
         if (_this.data.colorCircleFirst == '#FFDF2F') {
          _this.setData({
           colorCircleFirst: '#FE4D32',
           colorCircleSecond: '#FFDF2F',
          })
         } else {
          _this.setData({
           colorCircleFirst: '#FFDF2F',
           colorCircleSecond: '#FE4D32',
          })
         }
        }, 2000)
                //奖品item设置
            var awardList = [];
            //间距,怎么顺眼怎么设置吧.
            var topAward = 25;
            var leftAward = 25;
            for (var j = 0; j < 8; j++) {
            if (j == 0) {
            topAward = 25;
            leftAward = 25;
            } else if (j < 3) {
            topAward = topAward;
            //166.6666是宽.15是间距.下同
            leftAward = leftAward + 166.6666 + 15;
            } else if (j < 5) {
            leftAward = leftAward;
            //150是高,15是间距,下同
            topAward = topAward + 150 + 15;
            } else if (j < 7) {
            leftAward = leftAward - 166.6666 - 15;
            topAward = topAward;
            } else if (j < 8) {
            leftAward = leftAward;
            topAward = topAward - 150 - 15;
            }
            var imageAward = this.data.imageAward[j];
            awardList.push({ topAward: topAward, leftAward: leftAward, imageAward: imageAward });
            }
            this.setData({
            awardList: awardList
            })
    },
    //开始游戏
    startGame: function () {
        let that = this;
     if (this.data.isRunning) 
    {
        return false;
    }else{
        this.setData({
        isRunning: true,
        open:-1
        })
        var i = 0;
        
        var timer1 = setInterval(function () {
            i++;
            if(i>=2){
                clearInterval(timer1);   
                that.setData({
                    backs:1
                })
            that.goLottery()
            }
        },500)
        
      
     }
    },
    goLottery:function(){
        var _this = this;
        var indexSelect = 0
        var i = 0;
        var timer = setInterval(function () {
            indexSelect++;
            //这里我只是简单粗暴用y=30*x+200函数做的处理.可根据自己的需求改变转盘速度
            i += 30;
            if (i > _this.data.times * 8 * 30 + _this.data.index * 30 - 30) {
            // if (i > 1000) {
            //去除循环
            _this.setData({
                open:1,
                indexSelect: 99
            })
            clearInterval(timer);       
            _this.sets();
            }
            indexSelect = indexSelect % 8;
            _this.setData({
                 indexSelect: indexSelect
            })
            }, (100 + i))
    },
    sets:function(){
        this.setData({
            indexSelect:99
        })
    },
    Hide(){
        this.setData({
            show:false
        })
        setTimeout(this.gos,5000)
    },
    gos(){
        var limit=this.data.limit;
        if(limit==1){
            wx.reLaunch({
              url: '../mine/index',
            })
        }else{
            wx.navigateBack();
        }
    },
    openLottery:function(e){
        console.log(e)
        let that=this;
        var open=this.data.open;
        var id=e.currentTarget.dataset.id;
        var css='bk'+id;
        if(open==1){
            //console.log('open=>'+open)
            let that = this;
            this.setData({
                open:0
            })
        var open=wx.getStorageSync('open');
        const W = wx.getSystemInfoSync().windowWidth;
        const H = wx.getSystemInfoSync().windowHeight;
        const newYearImgs = ['/images/money.png', '/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png','/images/money.png'];
        const audio = wx.createInnerAudioContext();
        audio.src = 'https://gxm-ecommerce.oss-cn-shenzhen.aliyuncs.com/user_upload/rc-upload-1547213682422-20.mp3';
        audio.play();
        let ctx = wx.createCanvasContext('myCanvas');
         that.setData({
            ctx:ctx,
            show:true,
            canvasHeight:H
         })
                fall(ctx, W, H, newYearImgs, {
                    // duration: 15,
                    G: 8
                  });
        wx.request({
            url: app.globalData.platUrl+'lottery/open',
            data:{from:1,user_id:open.user_id,id:id},
            success:res=>{
                console.log(res);
                setTimeout(that.Hide,3000)
                that.setData({
                    backs:0,
                    awardList:res.data.awardList,

                })
                //wx.navigateBack();
            }
        })
        }else if(open==0){
            wx.showModal({
                title: '系统提示',
                content: '请不要重复点击',
                complete: (res) => {
                    console.log(res);
                  if (res.cancel) {
                    
                  }
              
                  if (res.confirm) {
                    
                  }
                }
              }) 
        }else{   
           wx.showModal({
             title: '系统提示',
             content: '等待开始',
             complete: (res) => {
                 console.log(res);
               if (res.cancel) {
                 
               }
           
               if (res.confirm) {
                 
               }
             }
           }) 
        }
    }
   })
  